package com.zk.monitormanagement.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zk.monitormanagement.R;
import com.zk.monitormanagement.activity.realplay.EZRealPlayActivity;
import com.zk.monitormanagement.base.BaseActivityImp;
import com.zk.monitormanagement.bean.LoginBean;
import com.zk.monitormanagement.constant.ManageConstants;
import com.zk.monitormanagement.utils.SharedPreferUtils;
import com.zk.monitormanagement.utils.ToastUtil;

import butterknife.BindView;

public class LoginActivity extends BaseActivityImp {

    @BindView(R.id.et_login_name)
    EditText etLoginName;
    @BindView(R.id.iv_login_delete)
    ImageView ivLoginDelete;
    @BindView(R.id.et_login_pwd)
    EditText etLoginPwd;
    @BindView(R.id.iv_login_delete_pwd)
    ImageView ivLoginDeletePwd;
    @BindView(R.id.iv_login)
    Button ivLogin;
    @BindView(R.id.btn_find_pwd)
    TextView btnFindPwd;
    @BindView(R.id.ll_login)
    LinearLayout llLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (ImmersionBar.isSupportStatusBarDarkFont()) { //判断当前设备支不支持状态栏字体变色
            //处理状态栏字体为黑色
            mImmersionBar.statusBarDarkFont(true).init();   //状态栏字体是深色，不写默认为亮色;
        } else {
            //处理状态栏有透明度
            mImmersionBar.barColor(R.color.blue).init();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        ivLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(etLoginName.getText().toString())){
                    ToastUtil.show("手机号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(etLoginPwd.getText().toString())){
                    ToastUtil.show("密码不能为空");
                    return;
                }
                getNetLogin();
            }
        });

        ivLoginDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etLoginName.setText("");
            }
        });

        ivLoginDeletePwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etLoginPwd.setText("");
            }
        });
        btnFindPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(FindPasswordActivity.class);
            }
        });
    }

    private void getNetLogin() {
        startProgressDialog();
        OkGo.<String>get(ManageConstants.Login_url)
                .params("mobile",etLoginName.getText().toString())
                .params("password",etLoginPwd.getText().toString())
                .params("devicetype","0")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        stopProgressDialog();
                        Gson gson=new Gson();
                        LoginBean loginBean=gson.fromJson(response.body(),LoginBean.class);
                        if (loginBean.getStatus().equals("1")){
                            SharedPreferUtils.getInstance().put(LoginActivity.this,"unitid",loginBean.getData().getUnitid());
                            SharedPreferUtils.getInstance().put(LoginActivity.this,"token",loginBean.getToken());
                            startThenKill(EZRealPlayActivity.class);
                        }else {
                            ToastUtil.show(loginBean.getMsg());
                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        stopProgressDialog();
                    }
                });
    }
}
